package com.cskaoyan.controller.admincontroller;

import com.cskaoyan.util.ValidationUtil;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.chaosbean.config.bo.*;
import com.cskaoyan.exception.ValidationException;
import com.cskaoyan.service.adminservice.ConfigService;
import com.cskaoyan.util.ReflectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author 楚夏三分
 * @datetime 2022/12/29 21:10
 * @description: TODO:
 */
@RestController
@RequestMapping("admin/config")
public class ConfigController {
    @Autowired
    ConfigService configService;

    @GetMapping("mall")
    public BaseRespVo getMall(){
        ArrayList<String> list = ReflectUtil.getConfigNamesByClazz(AdminConfigMallBo.class);
        HashMap<String, String> map = configService.list(list);
        return BaseRespVo.ok(map);
    }

    @PostMapping("mall")
    public BaseRespVo postMall(@RequestBody @Valid AdminConfigMallBo adminConfigMallBo, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);

        HashMap<String, String> map = ReflectUtil.getConfigUpdateMsgMapByClazz(adminConfigMallBo);
        configService.update(map);
        return BaseRespVo.ok();
    }

    @GetMapping("express")
    public BaseRespVo getExpress(){
        ArrayList<String> list = ReflectUtil.getConfigNamesByClazz(AdminConfigExpress.class);
        HashMap<String,String> result=  configService.list(list);
        return BaseRespVo.ok(result);
    }

    @PostMapping("express")
    public BaseRespVo postExpress(@RequestBody @Valid AdminConfigExpress adminConfigExpress, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);

        HashMap<String, String> map = AdminConfigExpress.getMap(adminConfigExpress);
        configService.update(map);
        return BaseRespVo.ok();
    }

    @GetMapping("order")
    public BaseRespVo getOrder(){
        ArrayList<String> list = ReflectUtil.getConfigNamesByClazz(AdminConfigOrderBo.class);
        HashMap<String,String> result=  configService.list(list);
        return BaseRespVo.ok(result);
    }

    @PostMapping("order")
    public BaseRespVo postOrder(@RequestBody @Valid AdminConfigOrderBo adminConfigOrderBo, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);

        HashMap<String, String> map = AdminConfigOrderBo.getMap(adminConfigOrderBo);
        configService.update(map);
        return BaseRespVo.ok();
    }

    @GetMapping("wx")
    public BaseRespVo getWX(){
        ArrayList<String> list = ReflectUtil.getConfigNamesByClazz(AdminConfigWXBo.class);
        HashMap<String,String> result=  configService.list(list);
        return BaseRespVo.ok(result);
    }

    @PostMapping("wx")
    public BaseRespVo postWX(@RequestBody @Valid AdminConfigWXBo adminConfigWXBo, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);

        HashMap<String, String> map = AdminConfigWXBo.getMap(adminConfigWXBo);
        configService.update(map);
        return BaseRespVo.ok();
    }



}
